
$('.form-horizontal').validate({
    rules: {
        username: {
            required: true, 
            minlength: 4, 
            maxlength: 12, 
        },
        password: {
            required: true, 
            minlength: 6, 
            maxlength: 16, 
        },
        reglogCode:{
            required: true,
        }
    },
    messages: {
        username: {
            required: '用户名不能为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大12位', 
        },
        password: {
            required: '密码为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大16位', 
        },
        reglogCode:{
            required: '验证码不能为空', 
        }
    },
// 表单提交事件
submitHandler: function (form) {
        let loadcode =  $('#reglogCode')[0].value 
        var captcha = getCookie('captcha')  
        if(loadcode.toUpperCase() == captcha.toUpperCase()){
            let formData = $('.form-horizontal').serialize()
            let uname = $('[name="username"]')[0].value
            let loadid = layer.load(1, {
                shade: [0.5, '#000']
            })
            sendAjax({
                method: 'post',
                url: '/users/api/login',
                data: formData,
                success: res => {
                    let {
                        errorCode,
                        message
                    } = res
                    if (errorCode == 0) {
                        layer.msg(message, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            setCookie('username',uname,7200)
                            setCookie('userid',res.data.userid,7200)
                            removeCookie('captcha')
                            var url = window.localStorage.getItem('url')
                            window.localStorage.setItem('token',res.data.token)
                            layer.close(loadid)
                            if(url){
                                window.localStorage.removeItem('url')
                                location.href = url
                            }else{
                                location.href = '/'
                            }
                            
                        })

                    } else {
                        layer.msg(message, {
                            icon: 2,
                            time: 2000
                        })
                        layer.close(loadid)

                    }
                }
            })
        }else{
        layer.msg('验证码错误请重新输入！', {
            icon: 2,
            time: 2000
        })
    }
    }
    }
)

// 点击图片刷新验证码
$('.loadcode').on('click',function(){
    this.src =
   '/users/api/reglogCode?' +  Math.random();
})